/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.protocol;

import cgl.narada.util.ByteUtilities;
/**
 * Protocol packets, especially those notifying the addition, deletion of 
 * gateways, nodes and address generation requests need to have unique ID 
 * associated with it. This class is responsible for the generation of unique
 * IDs and serves as a factory for the generation of these ID's. 
 *
 * @author Shrideep Pallickara
 * $Revision$
 * $Date$
 *  
 */ 

public class ProtocolIDFactory {
  
  /** This is derived from the Node Address of the node in question */
  private int nodeIdentifier;
  /** This refers to the incarnation of the server node in question */
  private short incarnation;
  /** Sequence numbers starting from zero */
  private int seqNum;
  
  private byte[] nodeIdentifierBytes;
  private byte[] incarnationBytes;

  /** Initializes the protocolID factory. We need both the nodeAddress of the
      system and the incarnation associated with the node to uniquely
      create protocolID's.
      @param nodeAddress The node address of the system.
      @param incarnation The incarnation associated with the node .
  */
  public ProtocolIDFactory(NodeAddress nodeAddress, short incarnation) {
    int[] addressInInts = nodeAddress.getAddressInInts();
    int level = addressInInts.length;
    for (int i=0; i < level; i++) {
      nodeIdentifier += addressInInts[level-1-i]*((int)Math.pow(32, i));
    }
    this.incarnation = incarnation;
    seqNum = 0;
    nodeIdentifierBytes = ByteUtilities.getBytes(nodeIdentifier);
    incarnationBytes    = ByteUtilities.getBytes(incarnation);
   
  }

  /** This method returns the next logical ProtocolID from the IDFactory.
      The ID's generated are unique system wide, and are used to uniquely
      identify new connections that are added, and so on.
      @return A unique protocolID.
  */
  public synchronized ProtocolID getProtocolID() {
    seqNum++;
    int offset =0;
    byte[] idBytes = new byte[4 /*nodeIdentifier */ + 2 /* incarnation */ +
			     4 /* seq Num */];
    System.arraycopy(nodeIdentifierBytes, 0, idBytes, offset, 4);
    offset+=4;
    System.arraycopy(incarnationBytes, 0, idBytes, offset, 2);
    offset+=2;
    System.arraycopy(ByteUtilities.getBytes(seqNum), 0, idBytes, offset, 4);
    return new ProtocolID(idBytes);
  }
  
  public static void main(String[] args) {
    int[] address = {30, 30, 30, 31};
    NodeAddress nodeAddress = new NodeAddress(address);
    ProtocolIDFactory factory = new ProtocolIDFactory(nodeAddress, (short)4 );
    
    byte[] nodeVal = new byte[4];
    byte[] incVal  = new byte[2];
    byte[] seqNumVal = new byte[4];
    for (int i =0; i < 5; i++) {
      byte[] idBytes = factory.getProtocolID().getBytes();
      System.arraycopy(idBytes, 0, nodeVal, 0, 4);
      System.arraycopy(idBytes, 4, incVal, 0, 2);
      System.arraycopy(idBytes, 6, seqNumVal, 0, 4);
      
      System.out.println( "Node Identifier = "+
			  ByteUtilities.getInt(nodeVal) +
			  " Incarnation  = " + 
			  ByteUtilities.getShort(incVal)+
			  " seqNum = " + ByteUtilities.getInt(seqNumVal));
    }    
  }
}



